<template>
	<div style="margin-bottom: 15px;">
		<el-breadcrumb :separator-icon="ArrowRight">
			<el-breadcrumb-item @click="toCarList(carParentData.name)">
				<a>{{carParentData.name}}</a>
			</el-breadcrumb-item>
			<el-breadcrumb-item @click="toCarListSon(carData.id)">
				<a>{{carData.name}}</a>
			</el-breadcrumb-item>
			<el-breadcrumb-item>
				{{InformationData.title}}
			</el-breadcrumb-item>
		</el-breadcrumb>
	</div>
	<div>
		<el-card class="!border-none mb-4" shadow="never">
			<el-form ref="formRef" class="mb-[-16px]" :model="queryParams" inline>
				<el-form-item label="目录/文件名称" prop="name">
					<el-input class="w-[280px]" v-model="queryParams.name" clearable placeholder="请输入目录/文件名称" />
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="getLists">查询</el-button>
					<el-button @click="resetParams">重置</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card class="!border-none" shadow="never">
			<div>
				<el-button v-perms="['information_info/add']" type="primary" @click="handleAdd()">
					<template #icon>
						<icon name="el-icon-Plus" />
					</template>
					新增
				</el-button>
				<el-button @click="handleExpand"> 展开/折叠 </el-button>
			</div>
			<div class="mt-4">
				<el-table v-loading="loading" ref="tableRef" class="mt-4" size="large" :data="lists" row-key="id"
					:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"  default-expand-all>
					<!-- <el-table-column label="编号" prop="id" show-overflow-tooltip /> -->
					<el-table-column label="目录/文件名称" prop="name" show-overflow-tooltip />
					<el-table-column label="类型" prop="" show-overflow-tooltip>
						<template #default="{ row }">
							<el-tag v-if="row.file_url!=''" type="success">文件</el-tag>
							<el-tag v-else type="primary">目录</el-tag>
						</template>
					</el-table-column>
					<el-table-column label="操作" width="160" fixed="right">
						<template #default="{ row }">
							<el-button v-perms="['information_info/add']" type="primary" link
								@click="handleAdd(row.id)" v-if="row.file_url==''">
								新增
							</el-button>
							<el-button v-perms="['information_info/edit']" type="primary" link @click="handleEdit(row)">
								编辑
							</el-button>
							<el-button v-perms="['information_info/delete']" type="danger" link
								@click="handleDelete(row.id)">
								删除
							</el-button>
						</template>
					</el-table-column>
				</el-table>
			</div>
		</el-card>
		<edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
	</div>
</template>

<script lang="ts" setup name="informationInfoLists">
	import { timeFormat } from '@/utils/util'
	import { useDictData } from '@/hooks/useDictOptions'
	import { apiInformationInfoLists, apiInformationInfoDelete } from '@/api/information_info'
	import {apiInformationListDetail } from '@/api/information_list'
	import feedback from '@/utils/feedback'
	import EditPopup from './edit.vue'
	import type { ElTable, FormInstance } from 'element-plus'
	import { useRoute, useRouter } from "vue-router"
	import { apiCarBrandDetail, apiCarBrandGetParent } from '@/api/car_brand'
	import { ArrowRight } from '@element-plus/icons-vue'

	const tableRef = shallowRef<InstanceType<typeof ElTable>>()
	const formRef = shallowRef<FormInstance>()
	const editRef = shallowRef<InstanceType<typeof EditPopup>>()
	let isExpand = false
	const route = useRoute();
	const router = useRouter();

	// 是否显示编辑框
	const showEdit = ref(false)
	const loading = ref(false)
	const lists = ref<any[]>([])
	let car_id = ref('')
	let information_list_id = ref('')

	// 查询条件
	const queryParams = reactive({
		pid: '',
		name: '',
		car_id: '',
		information_list_id:''
	})
	const carData = reactive({
		id: '',
		name: ''
	})
	const carParentData = reactive({
		id: '',
		name: ''
	})
	const InformationData = reactive({
		id: '',
		title: ''
	})
	onMounted(() => {
		if (route.query.car_id !== undefined) {
			car_id = route.query.car_id
			information_list_id = route.query.information_list_id
			console.log(route.query.id)
			queryParams.car_id = route.query.car_id
			queryParams.information_list_id = route.query.information_list_id
			getLists()
			getCarDetail(route.query.car_id)
			getInformation(route.query.information_list_id)
		} else {
			router.push('/information_list')
		}
	})
	const toCarList = async (search : string) => {
		router.push('/car_list?search=' + search)
	}
	const toCarListSon = async (id : string) => {
		router.push('/information_list?id=' + id)
	}


	const getCarDetail = async (id : number) => {
		const data = await apiCarBrandDetail({
			id: id
		})
		getCarParent(id)
		for (const key in carData) {
			if (data[key] != null && data[key] != undefined) {
				//@ts-ignore
				carData[key] = data[key]
			}
		}
		console.log(carData)
	}

	const getCarParent = async (id : number) => {
		const data = await apiCarBrandGetParent({
			id: id
		})
		for (const key in carParentData) {
			if (data[key] != null && data[key] != undefined) {
				//@ts-ignore
				carParentData[key] = data[key]
			}
		}
		console.log(carParentData.name)

	}
	
	const getInformation = async (id : number) => {
		const data = await apiInformationListDetail({
			id: id
		})
		for (const key in InformationData) {
			if (data[key] != null && data[key] != undefined) {
				//@ts-ignore
				InformationData[key] = data[key]
			}
		}
	
	}

	const resetParams = () => {
		formRef.value?.resetFields()
		getLists()
	}

	const getLists = async () => {
		loading.value = true
		try {
			const data = await apiInformationInfoLists(queryParams)
			lists.value = data.lists
			loading.value = false
		} catch (error) {
			loading.value = false
		}
	}

	// 选中数据
	const selectData = ref<any[]>([])

	// 表格选择后回调事件
	const handleSelectionChange = (val : any[]) => {
		selectData.value = val.map(({ id }) => id)
	}

	// 获取字典数据
	const { dictData } = useDictData('')

	// 添加
	const handleAdd = async (id ?: number) => {
		showEdit.value = true
		await nextTick()
		if (id) {
			editRef.value?.setFormData({
				pid: id
			})
		}
		editRef.value?.open('add')
		editRef.value?.getAll({
			car_id:car_id,
			information_list_id:information_list_id
		})
		editRef.value?.getSon('')
	}

	// 编辑
	const handleEdit = async (data : any) => {
		showEdit.value = true
		await nextTick()
		editRef.value?.open('edit')
		editRef.value?.setFormData(data)
		editRef.value?.getAll({
			car_id:car_id,
			information_list_id:information_list_id
		})
		editRef.value?.getSon(data.id)
		
	}

	// 删除
	const handleDelete = async (id : number | any[]) => {
		await feedback.confirm('确定要删除？')
		await apiInformationInfoDelete({ id })
		getLists()
	}

	const handleExpand = () => {
		isExpand = !isExpand
		toggleExpand(lists.value, isExpand)
	}

	const toggleExpand = (children : any[], unfold = true) => {
		for (const key in children) {
			tableRef.value?.toggleRowExpansion(children[key], unfold)
			if (children[key].children) {
				toggleExpand(children[key].children!, unfold)
			}
		}
	}


	// getLists()
</script>